//
//  PGRecommmendViewController.m
//  youqu
//
//  Created by 彭鹏 on 16/10/23.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGRecommmendViewController.h"
#import "PGHTTPSessionManager.h"
#import <MJExtension/MJExtension.h>
#import <MJRefresh/MJRefresh.h>
#import "PGCategoryItem.h"
#import "PGUserItem.h"
#import "PGCategoryViewCell.h"
#import "PGUserViewCell.h"
#import "PGUserModel.h"

@interface PGRecommmendViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *categoryTableView;
@property (weak, nonatomic) IBOutlet UITableView *userTableView;


/**PGHTTPSessionManager*/
@property (nonatomic, weak) PGHTTPSessionManager *manager;
/**categorys*/
@property (nonatomic, strong) NSArray *categorys;

/**users*/
@property (nonatomic, strong) NSMutableArray *users;

/**<#anotation#>*/
@property (nonatomic, strong) PGCategoryItem *selectedCategoryItem;


@end

@implementation PGRecommmendViewController

static NSString *const categoryCellID = @"categoryCell";
static NSString *const userCellID = @"userCell";
#pragma mark -懒加载manager
- (PGHTTPSessionManager *)manager {
    if (_manager == nil) {
        _manager = [PGHTTPSessionManager manager];
    }
    return _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self initTableView];
    [self setupRefreshView];
}

- (void)initTableView {
    [_categoryTableView registerNib:[UINib nibWithNibName:NSStringFromClass([PGCategoryViewCell class]) bundle:nil] forCellReuseIdentifier:categoryCellID];
    [_userTableView registerNib:[UINib nibWithNibName:NSStringFromClass([PGUserViewCell class]) bundle:nil] forCellReuseIdentifier:userCellID];
    
    _categoryTableView.contentInset = UIEdgeInsetsMake(navigationBarHeight, 0, 0, 0);
    _userTableView.contentInset = UIEdgeInsetsMake(navigationBarHeight, 0, 0, 0);
    
    [self loadCategoryData];
}

- (void)setupRefreshView {
    MJRefreshNormalHeader *headerView = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewUserData)];
    self.userTableView.mj_header = headerView;
    
    MJRefreshAutoNormalFooter *footerView = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreUserData)];
    self.userTableView.mj_footer = footerView;
    
}

- (void)loadMoreUserData {
    PGHTTPSessionManager *manager = [PGHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"list";
    parameters[@"c"] = @"subscribe";
    parameters[@"page"] = @(++_selectedCategoryItem.pageNum);
    parameters[@"category_id"] = _selectedCategoryItem.userId;
    if (_selectedCategoryItem.pageNum > _selectedCategoryItem.totalPage){
        _userTableView.mj_footer.hidden = _selectedCategoryItem.pageNum > _selectedCategoryItem.totalPage;
        return;

    }
    
    [manager GET:PGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        [_userTableView.mj_footer endRefreshing];
        NSArray *users = [PGUserModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        [_users addObjectsFromArray:users];
        
        _selectedCategoryItem.users = _users;
        [self.userTableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}



- (void)loadNewUserData {
    PGHTTPSessionManager *manager = [PGHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"list";
    parameters[@"c"] = @"subscribe";
//    parameters[@"page"] = @(pageNum);
//    NSLog(@"%zd",item.userId);
    parameters[@"category_id"] = _selectedCategoryItem.userId;
    [manager GET:PGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
        [_userTableView.mj_header endRefreshing];
        _selectedCategoryItem.totalPage = [responseObject[@"total_page"] integerValue];
        _selectedCategoryItem.pageNum = 1;
        _users = [PGUserModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        _selectedCategoryItem.users = _users;
        [self.userTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _categoryTableView) {
        return _categorys.count;
    }
    return _selectedCategoryItem.users.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _categoryTableView) {
        PGCategoryViewCell *categoryCell = [tableView dequeueReusableCellWithIdentifier:categoryCellID forIndexPath:indexPath];
        PGCategoryItem *item = _categorys[indexPath.row];
        categoryCell.item = item;
       
        return categoryCell;
    }
    
    PGUserViewCell *userCell = [tableView dequeueReusableCellWithIdentifier:userCellID forIndexPath:indexPath];
    
    PGUserModel *userModel = _selectedCategoryItem.users[indexPath.row];
    userCell.item = userModel;
    return userCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _categoryTableView) {
        _selectedCategoryItem = _categorys[indexPath.row];
        if (_selectedCategoryItem.users) {
            
            [_userTableView reloadData];
        }else {
            [_userTableView.mj_header beginRefreshing];
//            [self loadNewUserData];
        }
        
    } else {
        
    }
}


- (void)loadCategoryData {
    PGHTTPSessionManager *manager = [PGHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"category";
    parameters[@"c"] = @"subscribe";
    //    NSLog(@"%zd",item.userId);
    [manager GET:PGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        _categorys = [PGCategoryItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        [self.categoryTableView reloadData];
        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.categoryTableView selectRowAtIndexPath:indexpath animated:YES scrollPosition:UITableViewScrollPositionNone];
        [self tableView:_categoryTableView didSelectRowAtIndexPath:indexpath];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _categoryTableView) {
        return 44;
    } else {
        return 60;
    }
}



@end
